# 需求 下载http://pic.netbian.com/4kmeinv/图片
# 注意乱码问题处理
import os

import requests
from lxml import etree

if __name__ == '__main__':
    # 爬取到页面源码数据
    url = 'http://pic.netbian.com/4kmeinv/'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.3'}
    response = requests.get(url=url, headers=headers)
    # 中文乱码时编码设置1
    response.encoding = 'gbk'
    page_text = response.text

    # 解析数据：src属性值 alt属性
    tree = etree.HTML(page_text)
    li_list = tree.xpath('//div[@class="slist"]/ul/li')

    if not os.path.exists('./piclibs'):
        os.makedirs('./piclibs')
    for li in li_list:
        img_src = 'http://pic.netbian.com/' + li.xpath('./a/img/@src')[0]
        img_name = li.xpath('./a/img/@alt')[0] + '.jpg'
        # 中文编码乱码处理2
        # img_name.encode('iso-8859-1').decode('gbk')
        print(img_src, img_name)
        # 请求获取图片二进制数据
        img_data = requests.get(url=img_src, headers=headers).content
        img_path = 'piclibs/' + img_name
        with open(img_path, 'wb') as fp:
            fp.write(img_data)
            print(img_name, '下载成功')
